package com.gec.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.model.entity.SysMenu;

import com.gec.model.entity.SysRoleMenu;

import com.gec.model.vo.AssginMenuVo;
import com.gec.model.vo.RouterVo;
import com.gec.system.mapper.SysMenuMapper;
import com.gec.system.mapper.SysRoleMenuMapper;
import com.gec.system.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.system.service.SysRoleMenuService;

import com.gec.system.util.MenuHelper;
import com.gec.system.util.RouterHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author lys
 * @since 2023-11-24
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;



    @Override
    public List<SysMenu> findMenuTree() {
        List<SysMenu> sysMenus = this.baseMapper.selectList(null);

        List<SysMenu> menuTree = MenuHelper.bulidTree(sysMenus);
        return menuTree;
    }

    @Override
    public List<SysMenu> findSysMenuByRoleId(Long id) {
        //查询全部菜单
        //获取所有status为1的权限列表
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<SysMenu> list = this.getBaseMapper().selectList(queryWrapper);


        //查询角色目前菜单权限
        QueryWrapper<SysRoleMenu> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("role_id",id);
        List<SysRoleMenu> sysRoleMenus = this.sysRoleMenuMapper.selectList(queryWrapper2);

        //取出角色目前菜单权限的所有id
        List<Long> ids = new ArrayList<>();
        sysRoleMenus.forEach(sysRoleMenu -> {
            ids.add(sysRoleMenu.getMenuId());
        });

        //遍历菜单列表，将含有id列表的菜单选中
        list.forEach(li->{
            if (ids.contains(li.getId())){
                li.setSelect(true);
            }else {
                li.setSelect(false);
            }
        });

        //将菜单树转换为权限树
        List<SysMenu> allMenus = MenuHelper.bulidTree(list);
        return allMenus;
    }

    @Override
    public void saveRoleAuth(AssginMenuVo assginMenuVo) {

        Long roleId = assginMenuVo.getRoleId();
        //先删除 原有的角色菜单权限
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        this.sysRoleMenuMapper.delete(wrapper);

        //保存新的角色权限
        List<Long> menuIdList = assginMenuVo.getMenuIdList();
        menuIdList.forEach(menu->{
            if (menu!=null){
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(menu);
                this.sysRoleMenuMapper.insert(sysRoleMenu);
            }
        });

    }


    @Override
    public List<RouterVo> findCountMenuById(Long id) {

        List<SysMenu> menuList = null;
        //如果id为1，则是超级管理员 直接查询全部菜单
        if (id==1){

            QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("sort_value");
            menuList = this.getBaseMapper().selectList(wrapper);
        }else {
            //其他id用户 获取菜单
            menuList = this.getBaseMapper().findMenuListByCountId(id);
        }


        //将菜单集合转换成菜单列表
        menuList = MenuHelper.bulidTree(menuList);

        //将菜单树转换成 路由树
        List<RouterVo> routerVos = RouterHelper.buildRouters(menuList);

        return routerVos;
    }

    @Override
    public List<String> findCountPermsList(Long id) {
        //超级管理员admin账号id为：8
        List<SysMenu> sysMenuList = null;

        if (id.longValue() == 8) {
            sysMenuList = this.baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1));
        } else {
            sysMenuList = this.baseMapper.findMenuListByCountId(id);
        }
        //创建返回的集合
        List<String> permissionList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if(sysMenu.getType() == 2){
                permissionList.add(sysMenu.getPerms());
            }
        }
        return permissionList;
//        List<String> bottons = this.getBaseMapper().findCountPermsList(id);
//        return bottons;
    }

    @Override
    public List<RouterVo> findCountMenuList(Long id) {

        // 我们约定 chaojiguanliyuan 是 超级管理员拥有所有的权限 id是8
        List<SysMenu> sysMenuList = null;

        if (id.longValue() == 8) {
            //a.  表示是超级管理员
            sysMenuList = baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1).orderByAsc("sort_value"));
        }
        else {
            // b. 其他非超级管理员的 用户
            sysMenuList = baseMapper.findMenuListByCountId(id);
        }

        //c.构建树形数据
        List<SysMenu> sysMenuTreeList = MenuHelper.bulidTree(sysMenuList);

        //TODO  MeunHelper返回的数据属性和 前端路由的属性不一致，所以要处理
        //d.构建路由
        List<RouterVo> routerVoList = RouterHelper.buildRouters(sysMenuTreeList);

        return routerVoList;
    }
}
